<template>
  <a-card :bordered="false" :title="modalTitle" v-show="visible">
    <div v-show="visible2">
      <a-row :gutter="48" slot="extra">
        <a-col :md="48" :sm="48">
          <span class="table-page-search-submitButtons" style="float: right" >
            <!--          <a-button style="margin-left: 8px" type="primary" @click="finish()">完成</a-button>-->
            <a-button style="margin-left: 8px" type="primary" @click="save()">保存</a-button>
            <a-button style="margin-left: 8px" @click="handleCancel()">返回</a-button>
          </span>
        </a-col>
      </a-row>
      <title-divider title="基本信息" width="90px"></title-divider>
      <a-form :form="form">
        <a-form-item v-show="false" >
          <a-input v-decorator="['id']" type="hidden"/>
          <a-input v-decorator="['sbId']" type="hidden"/>
          <a-input v-decorator="['sbPartId']" type="hidden"/>
          <a-input v-decorator="['repairId']" type="hidden"/>
        </a-form-item>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="编号"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol">
              <a-input
                disabled="disabled"
                v-decorator="['no']"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="分析时间"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol"
            >
              <a-date-picker
                style="width: 100%"
                v-decorator="['analyzeTime', {rules: [{required: true, message: '分析时间不能为空'}]}]"
                :format="BaseTool.Date.PICKER_NORM_DATE_PATTERN" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="故障部位"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol"
            >
              <a-input
                disabled
                style="width: 70%"
                v-decorator="['sbPartName']"/>
              <a-button type="primary" style="width: 30%" @click="handlePartSelect">选择</a-button>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="24" :md="24" :sm="24">
            <a-form-item
              label="故障现象"
              :labelCol="BaseTool.Constant.labelCol2"
              :wrapperCol="BaseTool.Constant.wrapperCol2">
              <a-textarea
                :auto-size="{ minRows: 5 }"
                v-decorator="['problemDesc', {rules: [{required: true, message: '分析时间不能为空'}]}]"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="24" :md="24" :sm="24">
            <a-form-item
              :labelCol="BaseTool.Constant.labelCol2"
              :wrapperCol="BaseTool.Constant.wrapperCol2"
              label="检查处理过程"
            >
              <a-textarea
                :auto-size="{ minRows: 5 }"
                v-decorator="['checkProcess', {rules: [{required: true, message: '检查处理过程不能为空'}]}]"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="24" :md="24" :sm="24">
            <a-form-item
              :labelCol="BaseTool.Constant.labelCol2"
              :wrapperCol="BaseTool.Constant.wrapperCol2"
              label="原因分析">
              <a-textarea
                :auto-size="{ minRows: 5 }"
                v-decorator="['reasonAnalysis', {rules: [{required: true, message: '维修报告不能为空'}]}]" />
            </a-form-item>
          </a-col>
        </a-row>
        <title-divider title="改进措施" width="90px"></title-divider>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="更新维护标准"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol">
              <a-checkbox :checked="changeRepairPlan" v-model="changeRepairPlan"></a-checkbox>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="24" :md="24" :sm="24">
            <a-form-item
              label="更新"
              v-show="changeRepairPlan"
              :labelCol="BaseTool.Constant.labelCol2"
              :wrapperCol="BaseTool.Constant.wrapperCol2"
            >
              <a-button
                v-show="changeRepairPlan"
                @ok="handleOk"
                @click="handleViewCheck()"
                type="default" >维护标准</a-button>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="24" :md="24" :sm="24">
            <a-form-item
              label="内容"
              v-show="changeRepairPlan"
              :labelCol="BaseTool.Constant.labelCol2"
              :wrapperCol="BaseTool.Constant.wrapperCol2"
            >
              <a-textarea
                :auto-size="{ minRows: 5 }"
                v-decorator="['changeRepairPlanContent']" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="更新备件计划"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol">
              <a-checkbox :checked="changeSparePlan" v-model="changeSparePlan"></a-checkbox>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="24" :md="24" :sm="24">
            <a-form-item
              label="更新"
              v-show="changeRepairPlan"
              :labelCol="BaseTool.Constant.labelCol2"
              :wrapperCol="BaseTool.Constant.wrapperCol2"
            >
              <a-button
                v-show="changeSparePlan"
                @ok="handleOk"
                @click="handleViewBom()"
                type="default" >备件bom</a-button>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="24" :md="24" :sm="24">
            <a-form-item
              label="内容"
              v-show="changeSparePlan"
              :labelCol="BaseTool.Constant.labelCol2"
              :wrapperCol="BaseTool.Constant.wrapperCol2"
            >
              <a-textarea
                :auto-size="{ minRows: 5 }"
                v-decorator="['changeSparePlanContent']" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="人员培训"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol">
              <a-checkbox :checked="userTeach" v-model="userTeach"></a-checkbox>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="24" :md="24" :sm="24">
            <a-form-item
              label="内容"
              v-show="userTeach"
              :labelCol="BaseTool.Constant.labelCol2"
              :wrapperCol="BaseTool.Constant.wrapperCol2"
            >
              <a-textarea
                :auto-size="{ minRows: 5 }"
                v-decorator="['userTeachContent']" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="设备改造升级"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol">
              <a-checkbox :checked="sbChange" v-model="sbChange"></a-checkbox>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="24" :md="24" :sm="24">
            <a-form-item
              label="内容"
              v-show="sbChange"
              :labelCol="BaseTool.Constant.labelCol2"
              :wrapperCol="BaseTool.Constant.wrapperCol2"
            >
              <a-textarea
                :auto-size="{ minRows: 5 }"
                v-decorator="['sbChangeContent']" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="12" :md="24" :sm="24">
            <a-form-item
              label="其他"
              :labelCol="BaseTool.Constant.labelCol"
              :wrapperCol="BaseTool.Constant.wrapperCol">
              <a-checkbox :checked="otherChange" v-model="otherChange"></a-checkbox>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="BaseTool.Constant.row.gutter">
          <a-col :lg="24" :md="24" :sm="24">
            <a-form-item
              label="内容"
              v-show="otherChange"
              :labelCol="BaseTool.Constant.labelCol2"
              :wrapperCol="BaseTool.Constant.wrapperCol2"
            >
              <a-textarea
                :auto-size="{ minRows: 5 }"
                v-decorator="['otherChangeContent']" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <detail-sb-check ref="detailSbCheckModal" @ok="handleOk"/>
    <detail-sb-bom ref="detailSbBomModal" @ok="handleOk"/>
    <part-info-select-modal ref="partInfoSelectModal" @selected="handlePartSelected"/>
  </a-card>
</template>

<script>
import { addRepairReason, updateRepairReason, finishRepairReason } from '@/api/repair/repair-reason'
import pick from 'lodash.pick'
import DetailSbBom from '@/views/sb/modelbom/modules/DetailSbBom'
import DetailSbCheck from '@/views/check/checkstandard/modules/DetailSbCheck'
import { fetchSbInfo } from '@/api/sb/info'
import PartInfoSelectModal from '@/views/part/info/modules/PartInfoSelectModal'

export default {
  name: 'BaseRepairReason',
  components: {
    DetailSbCheck,
    DetailSbBom,
    PartInfoSelectModal
  },
  data () {
    return {
      confirmLoading: false,
      modalTitle: null,
      currentTab: 0,
      changeRepairPlan: false,
      changeSparePlan: false,
      userTeach: false,
      sbChange: false,
      otherChange: false,
      addFlag: false,
      statusMap: {},
      typeMap: {},
      type: 0,
      form: this.$form.createForm(this),
      visible: false,
      visible2: false,
      model: null
      // 下拉框map
    }
  },
  props: {},
  created () {
    // 下拉框map
    this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.REPAIR_REASON_STATUS)
    this.typeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.REPAIR_REASON_TYPE)
  },
  methods: {
    changeType (value) {
      this.type = value
    },
    base (record, repairForm = {}) {
      this.visible = true
      this.visible2 = true
      this.currentTab = 0
      this.model = repairForm
      const { form: { setFieldsValue } } = this
      // 如果是空标识添加
      if (this.BaseTool.Object.isBlank(record)) {
        this.addFlag = true
        this.modalTitle = '添加'
        repairForm.repairId = repairForm.id
        this.$nextTick(() => {
          setFieldsValue(Object.assign(pick(repairForm, [
            'repairId',
            'sbId',
            'no'
          ])))
        })
        return
      }
      this.addFlag = false
      record.analyzeTime = this.BaseTool.Moment(record.analyzeTime, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
      this.changeRepairPlan = record.changeRepairPlan
      this.changeSparePlan = record.changeSparePlan
      this.userTeach = record.userTeach
      this.sbChange = record.sbChange
      this.otherChange = record.otherChange
      this.modalTitle = '编辑'
      if (this.BaseTool.Object.isBlank(record.id)) {
        this.addFlag = true
        this.modalTitle = '复制'
      }
      this.type = record.type
      // 日期处理
      this.$nextTick(() => {
        setFieldsValue(Object.assign(pick(record, [
          'id',
          'sbId',
          'sbPartId',
          'sbPartName',
          'repairId',
          'no',
          'analyzeTime',
          'problemDesc',
          'checkProcess',
          'reasonAnalysis',
          'improveMeasure',
          'changeRepairPlan: null',
          'changeRepairPlanContent',
          'changeSparePlan',
          'changeSparePlanContent',
          'type',
          'userTeach',
          'userTeachContent',
          'sbChange',
          'sbChangeContent',
          'otherChange',
          'otherChangeContent',
          'status'
        ])))
      })
    },
    next () {
      this.currentTab += 1
    },
    prev () {
      this.currentTab -= 1
    },
    save () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        values.changeRepairPlan = this.changeRepairPlan
        values.changeSparePlan = this.changeSparePlan
        values.userTeach = this.userTeach
        values.sbChange = this.sbChange
        values.otherChange = this.otherChange
        values.analyzeTime = this.BaseTool.Date.formatter(values.analyzeTime, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        if (this.addFlag) {
          addRepairReason(values)
            .then((res) => {
              this.confirmLoading = false
              this.handleCancel()
            }).catch(() => {
              this.confirmLoading = false
            })
        } else {
          updateRepairReason(values)
            .then(() => {
              this.handleCancel()
            }).catch(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    finish () {
      const { form: { validateFieldsAndScroll } } = this
      this.confirmLoading = true
      validateFieldsAndScroll((errors, values) => {
        if (errors) {
          this.confirmLoading = false
          return
        }
        // 日期处理
        values.changeRepairPlan = this.changeRepairPlan
        values.changeSparePlan = this.changeSparePlan
        values.userTeach = this.userTeach
        values.sbChange = this.sbChange
        values.otherChange = this.otherChange
        values.analyzeTime = this.BaseTool.Date.formatter(values.analyzeTime, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN)
        finishRepairReason(values)
          .then((res) => {
            this.confirmLoading = false
            this.handleCancel()
          }).catch(() => {
            this.confirmLoading = false
          })
      })
    },
    handleCancel (values) {
      this.visible = false
      this.confirmLoading = false
      this.form.resetFields()
      this.$emit('ok', values)
    },
    handleOk (values) {
      this.visible2 = true
    },
    handleViewCheck () {
      this.visible2 = false
      fetchSbInfo({ id: this.model.sbId }).then(res => {
        const modal = this.$refs.detailSbCheckModal
        modal.base(res.data)
      })
    },
    handleViewBom (record) {
      this.visible2 = false
      fetchSbInfo({ id: this.model.sbId }).then(res => {
        const modal = this.$refs.detailSbBomModal
        modal.base(res.data)
      })
    },
    handlePartSelect () {
      const sbId = this.form.getFieldValue('sbId')
      if (this.BaseTool.Object.isBlank(sbId)) {
        this.$message.error('请先选择设备')
      }
      this.$refs.partInfoSelectModal.base({ sbId })
    },
    handlePartSelected (keys, rows) {
      const [ key ] = keys
      const [ row ] = rows
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue(Object.assign({
          'sbPartId': key,
          'sbPartName': row.name
        }))
      })
    }
  }
}
</script>
<style>
  .ant-steps-item-process .ant-steps-item-icon>.ant-steps-icon {
    color: #fff !important;
  }
</style>
